//
//  MyWidgetListViewController.swift
//  TimMusic
//
//  Created by nkl on 2024/10/24.
//

import Foundation
import SwiftUI

class MyWidgetListViewController: BasicViewController {
    var viewModel: MyWidgetContentViewModel = .init()

    lazy var customBar: MyWidgetNavBar = MyWidgetNavBar()
    lazy var hostVc: UIHostingController = {
        let contentView = MyWidgetContentView(viewModel: viewModel)
        let hostingController = UIHostingController(rootView: contentView)
        hostingController.view.backgroundColor = .clear
        return hostingController
    }()

    override func addChildren() {
        super.addChildren()
        customBar.backButton.addTarget(self, action: #selector(popCurrentVc), for: .touchUpInside)
        customBar.manageButton.addTarget(self, action: #selector(manageWidgets(sender:)), for: .touchUpInside)

        view.backgroundColor = .hexColor("#202020")
        view.addSubview(customBar)
        view.addSubview(hostVc.view)
    }

    @objc func popCurrentVc() {
        navigationController?.popViewController(animated: true)
    }

    @objc func manageWidgets(sender: UIButton) {
        // 更新 MyWidgetContentView 的属性
        viewModel.isEditing.toggle()
        sender.isSelected = viewModel.isEditing
    }

    override func makeConstraints() {
        super.makeConstraints()
        customBar.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.height.equalTo(44)
        }

        hostVc.view.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(customBar.snp.bottom)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-80)
        }
    }
}
